{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Working with DataFrames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first we install hub\n",
    "# runtime enviroment\n",
    "\n",
    "!pip install hub"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note**: Restart the colab runtime as few packages has been updated or you may get error (<font color=\"red\">FileNotFoundError</font>)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hub\n",
    "from hub.schema import Primitive\n",
    "from tqdm import tqdm\n",
    "from hub import Dataset, transform, schema\n",
    "from time import time\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>contest</th>\n",
       "      <th>winner</th>\n",
       "      <th>loser</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>7995092012829590</td>\n",
       "      <td>64</td>\n",
       "      <td>61</td>\n",
       "      <td>2020-08-21 19:15:34.342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5179645355963421</td>\n",
       "      <td>54</td>\n",
       "      <td>59</td>\n",
       "      <td>2020-08-21 20:10:45.923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>3186990526737690</td>\n",
       "      <td>64</td>\n",
       "      <td>59</td>\n",
       "      <td>2020-08-21 22:19:37.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>6369489012402744</td>\n",
       "      <td>70</td>\n",
       "      <td>62</td>\n",
       "      <td>2020-08-21 22:19:37.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>3091106470863898</td>\n",
       "      <td>71</td>\n",
       "      <td>62</td>\n",
       "      <td>2020-08-21 22:19:37.525</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0           contest  winner  loser                     time\n",
       "0           0  7995092012829590      64     61  2020-08-21 19:15:34.342\n",
       "1           1  5179645355963421      54     59  2020-08-21 20:10:45.923\n",
       "2           2  3186990526737690      64     59  2020-08-21 22:19:37.525\n",
       "3           3  6369489012402744      70     62  2020-08-21 22:19:37.525\n",
       "4           4  3091106470863898      71     62  2020-08-21 22:19:37.525"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tabular data\n",
    "df = pd.read_csv(\"ps4-madden21.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# i want to keep 'contest' column\n",
    "my_schema = {\n",
    "    \"contest\": Primitive(dtype=\"float32\")  # Primitive handles numpy arrays\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "@transform(schema=my_schema)\n",
    "def load_transform(sample):\n",
    "    return {\n",
    "        \"contest\": sample\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = load_transform((df['contest']))  # pass the dataframe column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Computing the transormation: 154k items [00:36, 4.23k items/s]                       "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "elapsed time: 39.528635025024414\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "start = time()\n",
    "\n",
    "tag = \"mynameisvinn/pandas\"  # use your own tag/url\n",
    "ds2 = ds.store(tag)\n",
    "\n",
    "end = time()\n",
    "print(\"elapsed time:\", end - start)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
